package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/Unknwon/com"
	"strconv"
	"hytx_manager/pkg/setting"
	"hytx_manager/models"
	"net/http"
	"github.com/astaxie/beego/validation"
	"hytx_manager/pkg/app"
	"hytx_manager/pkg/e"
)

func GetMembers(c *gin.Context) {

	is_enabled := com.StrTo(c.Query("is_enabled")).MustInt()
	company_name := c.Query("company_name")
	is_grade := com.StrTo(c.Query("is_grade")).MustInt()
	begin_time := c.Query("begin_time")
	end_time := c.Query("end_time")
	page, _:= strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize)))
	data, paginate := models.GetMembers(is_enabled, begin_time,end_time, company_name, is_grade,  page,limit)
	vip ,_:= models.GetVipFeeConfigSelectResult()
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": gin.H{
			"paginate": paginate,
			"data":     data,
			"vipFeeConfig" :vip,
		},
	})
}

func GetMemberInfoById(c *gin.Context) {
	user_id := com.StrTo(c.Query("user_id")).MustInt()
	valid := validation.Validation{}
	valid.Min(user_id, 1,"user_id").Message("user_id必须大于1！")
	data := models.GetMemberInfoById(user_id)
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": gin.H{
			"data":     data,
		},
	})
}

func GetPersonalPageTemplateUser(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()
	valid :=  validation.Validation{}
	valid.Min(id, 1,"id").Message("id必须大于1！")
	data ,err:= models.GetPersonalPageTemplateUser(id)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
	}
	appG.ResponseMsg(200, "", data)

}

func GetMemberMarketing(c *gin.Context)  {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()
	valid :=  validation.Validation{}
	valid.Min(id, 1,"id").Message("id必须大于1！")
	data ,err:= models.GetUserMarketing(id)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
	}
	appG.ResponseMsg(200, "", data)
}

func MemberAble(c *gin.Context) {
	id := c.PostForm("id")
	status,_ := strconv.Atoi(c.PostForm("status"))
	models.DB.Table("users").Where("id=?",id).Update("is_enabled",status)
	success(c)
}